angular.module('inspinia', ['angularFileUpload']).controller('wechatPublicBatchEditCtrl', function ($scope, $http, SweetAlert, $state, $stateParams, i18nService, $timeout, FileUploader) {
  //数据源
  i18nService.setCurrentLang('zh-cn');


  $scope.titles = [{value: "", text: "请选择"}];
  $scope.pubNames = [{value: "", text: "请选择"}];
  $scope.batchInfo = {userType: ""};
  $scope.userType = angular.copy(initData.MSG_TO_USER_TYPE);
  $scope.userType.unshift({value: "", text: "请选择"});


  /**
   * 获取模板明细
   * @param id
   */
  $scope.getInfoById = function () {
    $http.post('publicAccountMsgBatch/info/' + $stateParams.id,
      null, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
      .success(function (result) {
        if (result.status) {
          $scope.detailInfo = result.data;
          $scope.msgInfo = JSON.parse($scope.detailInfo.msgData);
        } else {
          $scope.notice(result.msg);
        }
      }).error(function () {
      $scope.notice("系统异常！");
    });
  };

  $scope.getInfoById();



  /**
   * 提交公众号模板消息推送批次记录表
   */
  $scope.submitBatchInfo = function () {

    if ($scope.detailInfo.effectiveStartTime && $scope.detailInfo.effectiveEndTime) {
      var stime = new Date($scope.detailInfo.effectiveStartTime).getTime();
      var etime = new Date($scope.detailInfo.effectiveEndTime).getTime();
      if ((etime - stime) <= 0) {
        $scope.notice("结束时间必须大于开始时间");
        return;
      }
    }

    $http({
      url: 'publicAccountMsgBatch/updateTimeBlock',
      method: 'POST',
      data: $scope.detailInfo
    }).success(function (result) {
      if (result.status) {
        $scope.submitting = false;
        $state.transitionTo('sys.wechatPublicBatch', null, {reload: false});
        $scope.notice(result.msg);
      } else {
        $scope.submitting = false;
        $scope.notice(result.msg);
      }
    });

  }


});

